package com.jinwei.qunguanjia.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.domain.model.Sort;

import java.util.Arrays;
import java.util.List;

/**
 * 查询sql拼接工具类
 * 2020/12/10 15:50
 * @author yuanzs
 */
public class SearchUtils {

    public static <T> QueryWrapper<T> parseWhereSql(List<Condition> conditionList){
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (null != conditionList && conditionList.size() > 0) {
            for (Condition condition : conditionList) {
                if (StringUtils.isNotEmpty(condition.getValue())){
                    switch (condition.getType()) {
                        case "eq": queryWrapper.eq(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "ne": queryWrapper.ne(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "like": queryWrapper.like(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "likeLeft": queryWrapper.likeLeft(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "likeRight": queryWrapper.likeRight(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "notLike": queryWrapper.notLike(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "gt": queryWrapper.gt(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "lt": queryWrapper.lt(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "ge": queryWrapper.ge(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "le": queryWrapper.le(StringUtils.toUnderScoreCase(condition.getColumn()), condition.getValue());break;
                        case "in": queryWrapper.in(StringUtils.toUnderScoreCase(condition.getColumn()), Arrays.asList(condition.getValue().split(",")));break;
                        case "eqDate": queryWrapper.apply("date_format("+StringUtils.toUnderScoreCase(condition.getColumn())+",'%y%m%d') = date_format('"+condition.getValue()+"','%y%m%d')");break;
                        case "geDate": queryWrapper.apply("date_format("+StringUtils.toUnderScoreCase(condition.getColumn())+",'%y%m%d') >= date_format('"+condition.getValue()+"','%y%m%d')");break;
                        case "leDate": queryWrapper.apply("date_format("+StringUtils.toUnderScoreCase(condition.getColumn())+",'%y%m%d') <= date_format('"+condition.getValue()+"','%y%m%d')");break;
                        default:break;
                    }
                }
            }
        }
        return queryWrapper;
    }

    public static <T> void parseOrderBySql(QueryWrapper<T> queryWrapper, List<Sort> sortList){
        if (null != sortList && sortList.size() > 0){
            for (Sort sort : sortList) {
                if(Sort.ASC.equals(sort.getType())){
                    queryWrapper.orderByAsc(sort.getColumn());
                } else {
                    queryWrapper.orderByDesc(sort.getColumn());
                }
            }
        }
    }

    public static <T> void parseLimitSql(QueryWrapper<T> queryWrapper, Integer offset, Integer limit){
        queryWrapper.last("limit "+offset+","+limit+"");
    }
}
